package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Actor;
import com.wanmait.movie.service.ActorService;
import com.wanmait.movie.util.AjaxResult;
import com.wanmait.movie.util.JWTUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("manage/actor")
public class ManageActorController {
    @Resource
    private ActorService actorService;

    @GetMapping("getAll")
    public AjaxResult getAll() {
        return AjaxResult.success(actorService.getStar());
    }

    /**
     * 显示演员/导演列表
     */
    @GetMapping("list")
    public AjaxResult list(String keyword,Integer pageNum) {
        if (pageNum == null || pageNum == 0){
            pageNum = 1;
        }
        return AjaxResult.success("查询成功",actorService.getActorList(keyword,pageNum,5)).put("total",actorService.getActorList(keyword,pageNum,5).getTotal());
    }

    @GetMapping("findDirector")
    public AjaxResult findDirector(){
        return AjaxResult.success("查询成功",actorService.getDirectorList());
    }

    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Actor actor){
        if (!actorService.saveOrUpdate(actor)){
            return AjaxResult.error(505,"添加或修改错误");
        }
        return AjaxResult.success(actorService.saveOrUpdate(actor));
    }

    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        if(!actorService.removeById(id)){
            return AjaxResult.error(506,"删除错误");
        }
        return AjaxResult.success(actorService.removeById(id));
    }
}
